<%--
  Created by IntelliJ IDEA.
  User: 86134
  Date: 2024/10/9
  Time: 9:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>理财信息查询</title>
<style>
    table {
        width: 100%;
        border-collapse: collapse;
    }
    th, td {
        padding: 8px;
        text-align: left;
        border-bottom: 1px solid #ddd;
    }
    tr:nth-child(even) {background-color: #f2f2f2;}
    th {
        background-color: #4CAF50;
        color: white;
    }
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        // 加载所有理财产品
        loadAllProducts();

        // 搜索按钮点击事件
        $('#searchButton').click(function() {
            var keyword = $('#keyword').val();
            var risk = $('#risk').val();
            searchProducts(keyword, risk);
        });

        // 搜索功能
        function searchProducts(keyword, risk) {
            $.ajax({
                url: 'search',
                type: 'POST',
                data: {
                    keyword: keyword,
                    risk: risk
                },
                success: function(data) {
                    $('#productsTable').html(data);
                },
                error: function() {
                    alert('查询失败，请重试！');
                }
            });
        }

        // 加载所有理财产品
        function loadAllProducts() {
            $.ajax({
                url: 'queryAll',
                type: 'GET',
                success: function(data) {
                    $('#productsTable').html(data);
                },
                error: function() {
                    alert('加载失败，请重试！');
                }
            });
        }

        // 修改按钮点击事件
        $(document).on('click', '.editButton', function() {
            var productId = $(this).data('id');
            window.location.href = "edit.jsp?id=" + productId;
        });
    });
</script>
</head>
<body>
    <h2>理财信息查询</h2>
    <form>
        产品代码：<input type="text" id="keyword"><br>
        风险评级：<select id="risk">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select><br>
        <input type="button" id="searchButton" value="查询">
    </form>
    <table id="productsTable">
        <tr>
            <th>产品代码</th>
            <th>风险评级</th>
            <th>预期收益</th>
            <th>发售起始日</th>
            <th>发售截止日</th>
            <th>产品到期日</th>
            <th>操作</th>
        </tr>
        <c:forEach var="product" items="${products}">
            <tr>
                <td>${product.id}</td>
                <td>${product.risk}</td>
                <td>${product.income}</td>
                <td>${product.saleStarting}</td>
                <td>${product.saleEnd}</td>
                <td>${product.end}</td>
                <td>
                    <button class="editButton" data-id="${product.id}">修改</button>
                    <form action="delete" method="post" style="display: inline;">
                        <input type="hidden" name="id" value="${product.id}">
                        <button type="submit" onclick="return confirm('确认删除此理财产品吗？')">删除</button>
                    </form>
                </td>
            </tr>
        </c:forEach>
    </table>
    <a href="add.jsp">新增理财信息</a>
</body>
</html>
